import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter_printer/flutter_printer.dart';
import 'package:flutter_uikit/t2mini/bean/CardBean.dart';
import 'package:flutter_uikit/t2mini/bean/PayBean.dart';
 

class DiaoUtil {
  static Dio myDio;

  static Dio getDio() {
    if (myDio == null) {
      myDio = new Dio();
      DiaoUtil.myDio.options.contentType = "application/x-www-form-urlencoded";
      setInterceptors();
      return myDio;
    }
    return myDio;
  }

  static void setInterceptors() {
    myDio.interceptors
        .add(InterceptorsWrapper(onRequest: (RequestOptions options) {
      print(
          "\n================================= 请求数据 =================================");
      print("method = ${options.method.toString()}");
      print("url = ${options.uri.toString()}");
      print("headers = ${options.headers}");
      print("params = ${options.data.fields.toString()}");
    }, onResponse: (Response response) {
      print(
          "\n================================= 响应数据开始 =================================");
      Printer.printMapJsonLog("code = ${response.statusCode}");
      Printer.printMapJsonLog("data = ${response.data.toString()}");
      print(
          "================================= 响应数据结束 =================================\n");
    }, onError: (DioError e) {
      print(
          "\n=================================错误响应数据 =================================");
      print("type = ${e.type}");
      print("message = ${e.message}");
    }));
  }

//终端签到
  static Future<Response> sign(var parms) async {
    FormData formData = FormData.fromMap(new Map<String, dynamic>.from(parms));
    return await DiaoUtil.getDio()
        .post("http://192.168.40.212:9001/term/Pairing", data: formData);
  }

  static void sign2(var parms) async {
    FormData formData = FormData.fromMap(new Map<String, dynamic>.from(parms));
    Response re = await DiaoUtil.getDio()
        .post("http://192.168.40.212:9001/term/Pairing", data: formData);
    print(re);
  }

  //人员信息获取
  static Future<CardBean> cardGet(var parms) async {
    FormData formData = FormData.fromMap(new Map<String, dynamic>.from(parms));
    Response resp = await DiaoUtil.getDio()
        .post("http://192.168.40.212:9001/card/get", data: formData);
    CardBean cardBean = CardBean.fromJson(jsonDecode(resp.data));
    return cardBean;
  }

//智盘入账(支付)
  static Future<PayBean> dishPay(var parms) async {
    FormData formData = FormData.fromMap(new Map<String, dynamic>.from(parms));
    Response resp = await DiaoUtil.getDio()
        .post("http://192.168.40.212:9001/dish/pay", data: formData);
    PayBean cardBean = PayBean.fromJson(jsonDecode(resp.data));
    return cardBean;
  }
}
